home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / PCCardNetworkSample / MyPortCalls.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.8 KB  |  287 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MyPortCalls.c
  3.  
  4.     Contains:    Generic calls for working with the MyPortScanner sample, to allow a PC Card Enabler
  5.                 to force an OT port deregistration
  6.  
  7.     Written by: Rich Kubota    
  8.  
  9.     Copyright:    Copyright © 1998-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/16/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include <PCCardEnablerPlugin.h>
  26. #include <PCCardTuples.h>
  27. #include <Errors.h>
  28. #include <Devices.h>
  29. #include <Gestalt.h>
  30. #include <CodeFragments.h>
  31. #include <DriverServices.h>
  32. #include <OpenTptModule.h>
  33. #include <OpenTptPCISupport.h>
  34. #include <OpenTptDevLinks.h>
  35. #include "EnablerSample.h"
  36. #include "MyPortCalls.h"
  37.  
  38. #define  DEBUG    1
  39.  
  40.  
  41. #pragma export on
  42.  
  43. /* A 'shlb' OT driver does not require a driver description record, however, the implementation 
  44.     of this port register sample requires the description to be entered into the name registry
  45.     for the port scanner to find and use in registering the network services for the card.
  46.     The driverDescription is entered here so that it can be registered along with the other
  47.     port properties which this technique requires.
  48. */
  49. DriverDescription TheEnetDriverDescription = 
  50. {
  51.     kTheDescriptionSignature,            // Signature field of this structure
  52.     kVersionOneDriverDescriptor,        // Version of this data structure
  53.     {                                    // Type of Driver
  54.         "\p"kDriverName,                // Driver Name/Info String
  55.         kVersionMajor,                    // 1st part of version number in BCD
  56.         kVersionMinor,                    // 2nd & 3rd part share a byte
  57.         kVersionStage,                    // stage: dev, alpha, beta, or final
  58.         kVersionNonRel                    // non-released revision level
  59.     },
  60.     {                                    // OS Runtime Requirements of Driver
  61.         kDriverIsUnderExpertControl,    // Options for OS Runtime
  62.         "\p"kDriverName                    // Driver's name to the OS
  63.     },
  64.     {                                    // Apple Service API Membership
  65.         1,                                // Number of Services Supported
  66.         {                                // The List of Services
  67.             kServiceCategoryOpenTransport,        // Service Category Name
  68.             OTPCIServiceType(kOTEthernetDevice,    // This is an Ethernet device
  69.                               kOTFramingEthernet |        // Ethernet Packets
  70.                               kOTFramingEthernetIPX |    // IPX Packets
  71.                               kOTFraming8022,            // 802.2 Packets,
  72.                              false,        // it is not TPI
  73.                              true),        // it is DLPI
  74.             kVersionMajor,                // 1st part of version number in BCD
  75.             kVersionMinor,                // 2nd & 3rd part share a byte
  76.             kVersionStage,                // stage: dev, alpha, beta,  or final
  77.             kVersionNonRel                // non-released revision level
  78.         }
  79.     }
  80. };
  81.  
  82.  
  83.  
  84.  
  85. /*
  86.     The CreatePortProperties registers the various name registry properties that we
  87.     want to get registered.
  88.     
  89.     Register the NetworkDriverDescription so that the port scanner can find it and
  90.     use it's info to register the port.  This is similar to what the PCINetworkCard
  91.     PortScanner does.
  92.     
  93.     Register the "port-configured" property to tell the default PC Card Port Scanner
  94.     to not handle port registration of this device - the value is not important
  95.     to the default port scanner
  96.     
  97.     Finally, register the "port-module" property to hold the name of the driver to be
  98.     opened.  The use of the property only applies if you use the default
  99.     pc card port scanner to register the ethernet port.  To do this, remove the code
  100.     above to make the call to CallPortScanner. also comment out the code
  101.     to register the "port-configured" property.  The default scanner checks for the
  102.     port-module property, and if present, uses the name in the property as the module
  103.     name that support this device.  This is the means by which a number of different
  104.     PC Ethernet cards can be supported by the same driver module.
  105.     
  106. */
  107.  
  108. OSStatus CreatePortProperties(const RegEntryID *cardRef)
  109. {
  110.     OSStatus     err = noErr;
  111.     UInt16        portConfigProperty;
  112.         
  113. #if    DEBUG    
  114.     DebugStr("\pCustomCardEnabler:  CreatePortProperties");        
  115. #endif
  116.  
  117.         // first we register TheEnetDriverDescription so that our custom port scanner
  118.         // knows the attributes of the port to register
  119.     err = RegistryPropertyCreate(cardRef, kDescriptorProperty, &TheEnetDriverDescription,
  120.                                      sizeof(DriverDescription));
  121.     if (err != noErr)
  122.     {
  123. #if    DEBUG
  124.         DebugStr("\p RegistryPropertyCreate failed creating driver-descriptor property");
  125. #endif
  126.         return err;
  127.     }
  128.     
  129.     portConfigProperty = 0;
  130.         // by setting this property, you keep the default PCCard Port Scanner from
  131.         // registering this port as an ethernet device.
  132.     err = RegistryPropertyCreate(cardRef, kPortConfigured, &portConfigProperty, sizeof(short));
  133.     if (err != noErr)
  134.     {
  135. #if    DEBUG
  136.         DebugStr("\p RegistryPropertyCreate failed creating port-configured property");
  137. #endif
  138.     }
  139.     else
  140.     {
  141.             // specify the module name to specify for the default PC Card Port scanner to
  142.             // use to register this port.  Note that this only applies if the above
  143.             // port-configured property is not created.  If the property is created, then
  144.             // creating the port-module property is just another property but which will 
  145.             // not be used by the PC Card Port Scanner.
  146.         err = RegistryPropertyCreate(cardRef, kPortModule, &gDriverName, sizeof(gDriverName));
  147.         if (err != noErr)
  148.         {
  149. #if    DEBUG
  150.             DebugStr("\p RegistryPropertyCreate failed creating port-module property");
  151. #endif
  152.         }
  153.     }
  154.     return err;
  155. }
  156.  
  157.  
  158. /*
  159.     CallPortScanner looks for our port scanner and registers our port.
  160.     Note that the call takes no argument and distinguishes cards that
  161.     aren't registered by accessing the 'port-configured' property.  If
  162.     the property is not present, then the scanner will not register a 
  163.     port for the device.  If the property is present, and it's value is 0
  164.     then it will register a port.  If the value is 1, then it knows that 
  165.     it has already registered a port for this property. 
  166. */
  167. void CallPortScanner(void)
  168. {
  169.     THz                savedZone;
  170.     OSErr            err;
  171.     CFragConnectionID    connID;
  172.     PortScanProcPtr    portScanProcPtr;
  173.     CFragSymbolClass        symClass;
  174.     Str32            portScannerName = "\pOTScanPorts";
  175.     Str255            errName;
  176.     
  177.         // before we call the port scanner we need to check for the presence of OT.
  178.         // if OT is not present, then we are at start up and our port scanner will
  179.         // executed to register the port, later when OT is started up.  
  180.         // The port scanner is only called automatically at system start up 
  181.         // and when the system comes out of sleep. 
  182.  
  183.     if (!IsOpenTransportPresent())
  184.         return;
  185.         
  186. #if    DEBUG    
  187.             DebugStr("\p CallPortScanner called");
  188. #endif
  189.         // Calls to GetSharedLibrary for fragments which are registered in the System heaqp
  190.         // require that we set the correct zone context
  191.     savedZone = GetZone();
  192.     SetZone(SystemZone());
  193.     
  194.     err = GetSharedLibrary(kPascalPortScannerName, kPowerPCCFragArch, kReferenceCFrag, &connID,
  195.                             (Ptr*)&portScanProcPtr, (unsigned char*)&errName);
  196.     if (err == noErr)
  197.     {
  198.         err = FindSymbol(connID, portScannerName, (Ptr*)&portScanProcPtr, &symClass);
  199.         if (err == noErr)
  200.         {
  201.             if (portScanProcPtr != nil)
  202.                 (*portScanProcPtr)(kOTInitialScan);
  203.         }
  204.         else
  205.         {
  206. #if    DEBUG    
  207.             DebugStr("\p FindSymbol failed");
  208. #endif
  209.         }
  210.             // we're finished with the library
  211.         CloseConnection(&connID);
  212.     }
  213.     else
  214.     {
  215. #if    DEBUG    
  216.         DebugStr("\p GetSharedLibrary failed");
  217. #endif
  218.     }
  219.     
  220.         // restore the zone
  221.     SetZone(savedZone);
  222. }
  223.  
  224. void    EnablerOfflinePort(const RegEntryID *cardRef)
  225. {
  226.     THz                        savedZone;
  227.     OSErr                    err;
  228.     CFragConnectionID        connID;
  229.     PortOfflineProcPtr    portOfflineProcPtr;
  230.     CFragSymbolClass        symClass;
  231.     Str32                    offlinePortName = "\pOfflineThePort";
  232.     Str255                    errName;
  233.     
  234. #if    DEBUG    
  235.             DebugStr("\p EnablerOfflinePort called");
  236. #endif
  237.         // Calls to GetSharedLibrary for fragments which are registered in the System heaqp
  238.         // require that we set the correct zone context
  239.     savedZone = GetZone();
  240.     SetZone(SystemZone());
  241.     
  242.     err = GetSharedLibrary(kPascalPortScannerName, kPowerPCCFragArch, kReferenceCFrag, &connID,
  243.                             (Ptr*)&portOfflineProcPtr, (unsigned char*)&errName);
  244.     if (err == noErr)
  245.     {
  246.             // Find the exported proc ptr symbol
  247.         err = FindSymbol(connID, offlinePortName, (Ptr*)&portOfflineProcPtr, &symClass);
  248.         if (err == noErr)
  249.         {
  250.             if (portOfflineProcPtr != nil)
  251.                 (*portOfflineProcPtr)(cardRef);
  252.         }
  253.         else
  254.         {
  255. #if    DEBUG    
  256.             DebugStr("\p FindSymbol on OfflineThePort failed");
  257. #endif
  258.         }
  259.             // we're finished with the library
  260.         CloseConnection(&connID);
  261.     }
  262.     else
  263.     {
  264. #if    DEBUG    
  265.         DebugStr("\p GetSharedLibrary failed");
  266. #endif
  267.     }
  268.     
  269.         // restore the zone
  270.     SetZone(savedZone);
  271.  
  272. }
  273.  
  274. Boolean IsOpenTransportPresent(void)
  275. {
  276.     long    result;
  277.     OSErr    err;
  278.     
  279.     err = Gestalt(gestaltOpenTptVersions, &result);
  280.     if (err == noErr)
  281.         return true;
  282.     else
  283.         return false;
  284. }
  285.  
  286.  
  287. #pragma export off